/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * KategorieFrame.java
 *
 * Created on 2011-12-31, 11:17:29
 */
package bd2;

import javax.swing.JOptionPane;
import java.util.ArrayList;

/**
 *
 * @author tolkjen
 */
public class KategorieFrame extends javax.swing.JFrame {
    
    private SignalReciever reciever;

    /** Creates new form KategorieFrame */
    public KategorieFrame() {
        initComponents();
    }
    
    public final Boolean loadKategorie() {
        ArrayList<Object[]> data = DataStore.getInstance().getKategorie();
        if (data != null) {
            katTable1.clear();
            for (int i=0; i<data.size(); ++i)
                katTable1.addRow(data.get(i));
            katTable1.updateUI();
            
            return true;
        } else {
            return false;
        }
    }
    
    public void show(SignalReciever sr) {
        reciever = sr;
        loadKategorie();
        setLocationRelativeTo(null);
        setVisible(true);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        katTable1 = new bd2.KatTable();
        jButton1 = new javax.swing.JButton();
        jButton2 = new javax.swing.JButton();

        setTitle("Wybierz kategorię");

        jScrollPane1.setViewportView(katTable1);

        jButton1.setText("Anuluj");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        jButton2.setText("Wybierz");
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap(374, Short.MAX_VALUE)
                .addComponent(jButton2)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jButton1)
                .addContainerGap())
            .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 512, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 266, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButton1)
                    .addComponent(jButton2))
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
        if (katTable1.getSelectedRowCount() > 0) {
            Integer row = katTable1.getSelectedRow();
            Integer Id = (Integer) katTable1.getRow(row)[0];
            String info = (String) katTable1.getRow(row)[1];
            reciever.signal("kategoria", new Object[]{Id, info});
        
            setVisible(false);
        } else {
            JOptionPane.showMessageDialog(null, "Wybierz kategorię!",
                    "Program", JOptionPane.INFORMATION_MESSAGE);
        }
    }//GEN-LAST:event_jButton2ActionPerformed

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        setVisible(false);
    }//GEN-LAST:event_jButton1ActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JScrollPane jScrollPane1;
    private bd2.KatTable katTable1;
    // End of variables declaration//GEN-END:variables
}
